package com.li.toolkit.utils;

import java.util.*;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2/19/2022 10:12 AM
 */
public final class MapUtils {

    private MapUtils() {
    }

    /**
     * 返回 Map
     *
     * @param object Object
     * @return Map
     */
    @SuppressWarnings("all")
    public static Map<String, Object> getMap(Object object) {
        if (object instanceof Map) {
            return (Map<String, Object>) object;
        }
        return Collections.emptyMap();
    }

    /**
     * 获得 Map
     *
     * @param map Map
     * @param key key
     * @return Map
     */
    public static Map<String, Object> getMap(Map<String, Object> map, String key) {
        if (Objects.isNull(map) || map.isEmpty()) {
            return Collections.emptyMap();
        }
        return getMap(map.get(key));
    }

    /**
     * 获取 Map 中的 list
     *
     * @param map 组件对象 Map
     * @param key map 中的 key
     * @return List<Map < String, Object>>
     */
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> getMapList(Map<String, Object> map, String key) {
        if (Objects.isNull(map) || Objects.isNull(map.get(key))) {
            return Collections.emptyList();
        }
        if (map.get(key) instanceof List) {
            List<Object> list = (List<Object>) map.get(key);
            List<Map<String, Object>> result = new ArrayList<>();
            for (Object object : list) {
                if (object instanceof Map) {
                    result.add((Map<String, Object>) object);
                }
            }
            return result;
        }
        return Collections.emptyList();
    }

    /**
     * 获取 Map 中的 list
     *
     * @param map 组件对象 Map
     * @param key map 中的 key
     * @return List<Object>
     */
    @SuppressWarnings("all")
    public static List<Object> getObjectList(Map<String, Object> map, String key) {
        if (Objects.isNull(map) || Objects.isNull(map.get(key))) {
            return Collections.emptyList();
        }
        if (map.get(key) instanceof List) {
            return (List<Object>) map.get(key);
        }
        return Collections.emptyList();
    }

}
